//
//  RepeatReminderViewController.m
//  安好
//
//  Created by rlp on 14-11-14.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "RepeatReminderViewController.h"

@interface RepeatReminderViewController ()

@end

@implementation RepeatReminderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"喝水提醒设置";
    // Do any additional setup after loading the view from its nib.
    self.edgesForExtendedLayout=UIRectEdgeNone;
    //左按钮“返回”
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backClicked)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnImg;

    
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self.tableView setBackgroundView:nil];
    [self.tableView setBackgroundColor:[ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16]];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

}
-(void)backClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UITableViewDelegate
//每一个section中含有几行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (kScreenWidth == 414?50*1.2:50);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"WaterRemindTableViewCell";
    
    WaterRemindTableViewCell *circleCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!circleCell) {
        if (kScreenWidth == 414) {
            circleCell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil]objectAtIndex:1];

        }else{
            circleCell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil]objectAtIndex:0];

        }
    }
    circleCell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    circleCell.isSwitch.tag =indexPath.row;
    [circleCell.isSwitch addTarget:self action:@selector(isSwitchChange:) forControlEvents:UIControlEventValueChanged];
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isSwithOn"] isEqualToString:@"yes"]) {
        [circleCell.isSwitch setOn:YES animated:YES];
    }else{
        [circleCell.isSwitch setOn:NO animated:YES];
        
    }
    if (indexPath.row == 0) {
        circleCell.titleLable.text = @"周六日不提醒";
    }
    circleCell.isSwitch.right = kScreenWidth-15;
    /**
     *  线条的处理
     */
    circleCell.topLineImg.backgroundColor = [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1.0];
    circleCell.topLineImg.height = 0.5;
    circleCell.topLineImg.width = kScreenWidth;
    circleCell.bottonLineImg.backgroundColor =[UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1.0];
    circleCell.bottonLineImg.frame = CGRectMake(0, (kScreenWidth == 414?50*1.2:50)-0.5, kScreenWidth, 0.5);
    circleCell.selectionStyle = UITableViewCellSelectionStyleNone;
    return circleCell;
}
-(IBAction)isSwitchChange:(UISwitch *)sender{
    
    switch (sender.tag) {
        case 0:
        {
            if (sender.on) {
                
                [[NSUserDefaults standardUserDefaults] setObject:@"yes" forKey:@"isSwithOn"];
            }else{
                [[NSUserDefaults standardUserDefaults] setObject:@"no" forKey:@"isSwithOn"];
                
            }
            AlarmClockRemind *al=[[AlarmClockRemind alloc]init];
            
            for (int i=0; i<self.clearArray.count; i++) {
                [AlarmClockRemind cancelLocalNotificationMark:[self.numArray objectAtIndex:i]];//取消通知
                
                NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                [formatter setDateFormat:@"HH:mm:ss"];
                
                NSDate *fromDate =[formatter dateFromString:[AlarmClockRemind getTimeHHmmWithHHmmss:[self.clearArray objectAtIndex:i]]];
                
                if (sender.on) {
                    [al addLocalNotificationsWithTimes:fromDate isWorkingDays:YES category:@"喝水提醒" alertBody:[self.numArray objectAtIndex:i] Mark:[self.numArray objectAtIndex:i]];
                    
                }else{
                    [al addLocalNotificationsWithTimes:fromDate isWorkingDays:NO category:@"喝水提醒" alertBody:[self.numArray objectAtIndex:i] Mark:[self.numArray objectAtIndex:i]];
                    
                }
            }
        }
            break;
            
        default:
            break;
    }
}





/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
@end
